//
//  CreateJobScheculeDateOfInstallVC.swift
//  GreenDeal
//
//  Created by Moennig Q on 2/3/2023.
//

import UIKit

class CreateJobScheculeDateOfInstallVC: BaseViewController {
    var editFlag: Bool = false//创建和Review的编辑共用此功能
    let customView = CreateJobScheculeDateOfInstallView(topBGImageName: "createjob_installdate_top_bg",useScrollViewFlag: false)
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initUI()
        self.loadData(self.customView.installDate, firstLoading: true)
        if self.editFlag {//编辑时，选择日期； （初始化时为今天不用选择上）
            self.customView.calendar.select(CreateJobModelHelper.shared.createJobViewModel.installDate)
        }
        
    }
    //
    private func initUI(){
        self.view = self.customView
        self.customView.setTitleInfo(R.string.localizable.scheduledDateOfInstall())
        self.customView.refreshJobsByDateClosure = {[weak self] (date) in
            guard let strongSelf = self else { return  }
            strongSelf.loadData(date, firstLoading: false)
        }
    }
    private func loadData(_ date: Date, firstLoading: Bool ){
        let service = CreateJobService()
        service.setBlockAfterNetwork { [weak self] (result, otherInfo) in
            guard let strongSelf = self else { return  }
            if let data = result as? [CreateJobStatusModel]{
                let completedList = data.filter({$0.status != PVDJobStatusName.scheduled.rawValue && $0.status != PVDJobStatusName.installing.rawValue && $0.status != PVDJobStatusName.unqualified.rawValue}).map({$0.install_date})
                let notCompletedList = data.filter({$0.status == PVDJobStatusName.scheduled.rawValue || $0.status == PVDJobStatusName.installing.rawValue || $0.status == PVDJobStatusName.unqualified.rawValue}).map({$0.install_date})
                //
                if firstLoading {//第一次加载设置安装日期为CreateJobModelHelper.shared.createJobViewModel.installDate
                    strongSelf.customView.installDate = CreateJobModelHelper.shared.createJobViewModel.installDate
                }
                strongSelf.customView.setData(notDoneList: notCompletedList,doneList: completedList)
            }
        } errorBlock: { code, error in
            Tool().showErrorInfo(code, error)
        }
        let beginDate = DateUtil().getServeralYearOfToday(date: date, month: -2)
        let endDate = DateUtil().getServeralYearOfToday(date: date, month: 2)
        service.getJobStatusList(beginDate, endDate: endDate)

    }
    func getData(){
        CreateJobModelHelper.shared.createJobViewModel.installDate = self.customView.installDate
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: false)
    }
}
